local hx__nitai = fk.CreateSkill {
  name = "hx__nitai",
  tags = { Skill.Switch},
}

local U = require "packages/utility/utility"

hx__nitai:addEffect(fk.DamageCaused, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__nitai.name) and data.card and data.card.trueName == "slash" then
      if player:getSwitchSkillState(hx__nitai.name, false) == fk.SwitchYang then
        return not player:isNude() and event == fk.DamageCaused
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local side = player:getSwitchSkillState(hx__nitai.name, false)
    if side == fk.SwitchYang then
      local card = room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        skill_name = hx__nitai.name,
        include_equip = true,
        prompt = "#nitai-discard",
      })
      if #card > 0 then
        event:setCostData(self, data.to)
        return true
      end
    else
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      if player:getSwitchSkillState(hx__nitai.name, true) == fk.SwitchYin then
        player:drawCards(1,hx__nitai.name)
        local card = player.room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          skill_name = hx__nitai.name,
          include_equip = true,
          prompt = "#nitai-discard2:::" .. data.card:toLogString(),
          cancelable = true,
          pattern = ".|.|heart",
        })
        if #card > 0 then
          room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = hx__nitai.name
          })
        end
      else
        room:addPlayerMark(event:getCostData(self), "@hx__nitai_heiyan", 1)
      end
  end,
})
hx__nitai:addEffect(fk.DamageInflicted, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__nitai.name) and data.card and data.card.trueName == "slash" then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local side = player:getSwitchSkillState(hx__nitai.name, false)
    if side == fk.SwitchYang then
      local card = room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        skill_name = hx__nitai.name,
        include_equip = true,
        prompt = "#nitai-discard",
      })
      if #card > 0 then
        event:setCostData(self, data.to)
        return true
      end
    else
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      if player:getSwitchSkillState(hx__nitai.name, true) == fk.SwitchYin then
        player:drawCards(1,hx__nitai.name)
        local card =  player.room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          skill_name = hx__nitai.name,
          include_equip = true,
          prompt = "#nitai-discard2:::" .. data.card:toLogString(),
          cancelable = true,
          pattern = ".|.|heart",
        })
        if #card > 0 then
          room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = hx__nitai.name
          })
        end
      else
        room:addPlayerMark(event:getCostData(self), "@hx__nitai_heiyan", 1)
      end
  end,
})

hx__nitai:addEffect(fk.GameStart, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__nitai.name)
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = hx__nitai.name, prompt = "拟态：是否切换【拟态】的阴阳状态？"})
  end,
  on_use = function(self, event, target, player, data)
    U.SetSwitchSkillState(player, hx__nitai.name)
  end,
})
hx__nitai:addEffect(fk.EventPhaseStart, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill("hx__nitai") and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = hx__nitai.name, prompt = "拟态：是否切换【拟态】的阴阳状态？"})
  end,
  on_use = function(self, event, target, player, data)
    U.SetSwitchSkillState(player, hx__nitai.name)
  end,
})


hx__nitai:addEffect(fk.TurnStart, {
  name = "hx__nitai_heiyan",
  anim_type = "negative",
  global = true,
  priority = 2,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target:getMark("@hx__nitai_heiyan") > 0 and player == target
  end,
  on_use = function(self, event, target, player, data)
    local num = target:getMark("@hx__nitai_heiyan")
    player.room:sendLog({ type = "#hx__nitai_heiyan_Log", from = target.id,arg = num})
    player.room:removePlayerMark(target,"@hx__nitai_heiyan",1)
    player.room:loseHp(target, num, hx__nitai.name)
  end,
})


return hx__nitai